package com.daqsoft.service.system;

import com.daqsoft.dao.GroupDao;
import com.daqsoft.data.Group;
import com.daqsoft.dto.UserManageResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户组管理服务层
 * @author 周麟东 on 9:38
 */
@Service
public class GroupService {

    @Autowired
    private GroupDao groupDao;
    @Autowired
    private UserManageService userManageService;

    public UserManageResultEnum save(Group group , Integer leaderId){
        //先查询是否有这个用户组
        if (group.getId()==0){
            Group have = groupDao.findGroupByGroupName(group.getGroupName());
            if (have!=null){
                return UserManageResultEnum.GROUP_NAME_REPEAT;
            }
        }
        if (group.getLevel() < 0){
            return UserManageResultEnum.LEVEL_ERR;
        }
        if (leaderId!=null  && leaderId!= -1){
            group.setLeader(userManageService.findOne(leaderId));
        }
        group = groupDao.save(group);
        return UserManageResultEnum.SUCCESS.setGroupData(group);
    }

    public List<Group> findAll(){
        return groupDao.findAll();
    }

    public void remove(int id){
        groupDao.delete(id);
    }

    public Group findOne(int id) {
        return groupDao.findOne(id);
    }
}
